<template>
    <div>
        <h2>G6 力导向布局（d3-force）示例</h2>
        <!-- 图渲染容器，id 用于 G6 绑定 -->
        <div id="container" style="width:100%;height:400px;background:#f5f5f5;"></div>
    </div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue';
import { Graph } from '@antv/g6';

defineOptions({ name: 'AntvG6Demo8' });

onMounted(() => {
    // 图数据，包含节点和边
    const data = {
        nodes: [
            { id: 'node0', size: 50 },
            { id: 'node1', size: 50 },
            { id: 'node2', size: 50 },
            { id: 'node3', size: 50 },
            { id: 'node4', size: 30, isLeaf: true },
            { id: 'node5', size: 30, isLeaf: true },
            { id: 'node6', size: 15, isLeaf: true },
            { id: 'node7', size: 15, isLeaf: true },
            { id: 'node8', size: 15, isLeaf: true },
            { id: 'node9', size: 15, isLeaf: true },
            { id: 'node10', size: 15, isLeaf: true },
            { id: 'node11', size: 15, isLeaf: true },
            { id: 'node12', size: 15, isLeaf: true },
            { id: 'node13', size: 15, isLeaf: true },
            { id: 'node14', size: 15, isLeaf: true },
            { id: 'node15', size: 15, isLeaf: true },
            { id: 'node16', size: 15, isLeaf: true },
        ],
        edges: [
            { source: 'node0', target: 'node1' },
            { source: 'node0', target: 'node2' },
            { source: 'node0', target: 'node3' },
            { source: 'node0', target: 'node4' },
            { source: 'node0', target: 'node5' },
            { source: 'node1', target: 'node6' },
            { source: 'node1', target: 'node7' },
            { source: 'node2', target: 'node8' },
            { source: 'node2', target: 'node9' },
            { source: 'node2', target: 'node10' },
            { source: 'node2', target: 'node11' },
            { source: 'node2', target: 'node12' },
            { source: 'node2', target: 'node13' },
            { source: 'node3', target: 'node14' },
            { source: 'node3', target: 'node15' },
            { source: 'node3', target: 'node16' },
        ],
    };

    // 创建 G6 图实例
    const graph = new Graph({
        container: 'container', // 直接用 id 绑定容器
        data,
        node: {
            style: {
                size: (d: any) => d.size, // 节点大小支持函数
            },
        },
        layout: {
            type: 'd3-force', // 力导向布局
            link: {
                distance: (d: any) => {
                    if (d.source.id === 'node0') {
                        return 100;
                    }
                    return 30;
                },
                strength: (d: any) => {
                    if (d.source.id === 'node1' || d.source.id === 'node2' || d.source.id === 'node3') {
                        return 0.7;
                    }
                    return 0.1;
                },
            },
            manyBody: {
                // 控制边的拉力强度
                strength: (d: any) => {
                    if (d.isLeaf) {
                        return -50;
                    }
                    return -10;
                },
            },
        },
        behaviors: ['drag-element-force'], // 支持节点拖拽和力导向联动
    });

    // 渲染图
    graph.render();
});
</script> 